/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Sep 3, 2013  2:26:48 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.interfaces.controller
 * File Name : GridUtils.java
 * TODO
 */
package com.tis.interfaces.controller;


import java.util.ArrayList;
import java.util.List;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Sep 3, 2013 : 2:26:48 PM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
public class GridUtils {

	/**
	 * Date =Sep 3, 2013 : 2:26:48 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public GridUtils() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * Date =Sep 3, 2013 : 2:28:30 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param dataSet
	 * @param rowsPerPage
	 * @return
	 */
	public static int getTotalNumberOfPages(List<?> dataSet, int rowsPerPage){
	    int totalNumberOfPages = (int) Math.ceil((double)dataSet.size() / (double)rowsPerPage);
	    return totalNumberOfPages;
	}

	/**
	 * 
	 * Date =Sep 3, 2013 : 2:28:34 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param dataSet
	 * @param pageNumber
	 * @param rowsPerPage
	 * @return
	 */
	public static int getCurrentPageNumber(List<?> dataSet, int pageNumber, int rowsPerPage){
		return pageNumber++;
	}

	/**
	 * 
	 * Date =Sep 3, 2013 : 2:28:41 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param dataSet
	 * @param pageNumber
	 * @param itemsPerPage
	 * @return
	 */
	public static <T> List<T> getDataForPage(List<T> dataSet, int pageNumber, int itemsPerPage){
		List<T> result = null;
		int fromIndex = 0;
		
		if(pageNumber > 1) {
			fromIndex = (pageNumber - 1) * itemsPerPage;
		}

		if(fromIndex >= dataSet.size()){
			result = new ArrayList<T>();
		}

		int endIndex = fromIndex + itemsPerPage;
		if(endIndex > dataSet.size()){
			result = dataSet.subList(fromIndex, dataSet.size());
		}
		else {
			result = dataSet.subList(fromIndex, endIndex);
		}
	return result;
  }

}
